package com.zhy.skinchangenow.image

import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import com.water.skin.SkinManager
import com.water.skin.callback.DefaultSkinChangingCallback
import com.water.skin.callback.ISkinChangingCallback
import com.zhy.skinchangenow.Constant
import com.zhy.skinchangenow.R
import com.zhy.skinchangenow.databinding.FragmentImageBinding
import java.lang.Exception


private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"


class ImageFragment : Fragment() {

    private var param1: String? = null
    private var param2: String? = null

    private lateinit var vBinding: FragmentImageBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            param1 = it.getString(ARG_PARAM1)
            param2 = it.getString(ARG_PARAM2)
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        vBinding = FragmentImageBinding.inflate(inflater, container, false)
        return vBinding.root
    }

    private var type = 0

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        SkinManager.getInstance().register(this)
        vBinding.run {
            btnUpdateA.setOnClickListener {
                Log.i("ImageFragment", "onActivityCreated: ")
                //ivChange.setTag(com.water.skin.R.id.skin_tag_id,if (type % 2 == 0) "skin:ic_fish:src" else "skin:ic_bottle_ball_camo:src")
                //ivTop.setTag(com.water.skin.R.id.skin_tag_id, "skin:ic_fish:src")

                type++
                /*ivTop.setTag(
                    com.water.skin.R.id.skin_tag_id,
                    if (type % 2 == 0) "skin:ic_fish:src" else "skin:ic_bottle_ball_camo:src"
                )*/
                //Constant.SKIN_PACKAGE,
                val skinTag =
                    if (type % 2 == 0) "skin:ic_fish:src" else "skin:ic_bottle_ball_camo:src"
                SkinManager.getInstance()
                    .skinTag(ivTop, skinTag)
                    .changeSkin(
                        Constant.SKIN_PATH,
                        callback = object : DefaultSkinChangingCallback() {

                            override fun onError(e: Exception) {
                                Toast.makeText(context, "换肤失败:" + e.message, Toast.LENGTH_SHORT)
                                    .show()
                            }

                            override fun onComplete() {
                                Toast.makeText(context, "换肤成功", Toast.LENGTH_SHORT).show()
                            }
                        })
            }

            btnUpdateB.setOnClickListener {
                type++
                val skinTag = if (type % 2 == 0) "skin:ic_fish_pet_left_normal:gifSrc"
                else "skin:ic_fish_pet_left_normal_water_lantern_blue:gifSrc"
                SkinManager.getInstance()
                    .skinTag(ivGif, skinTag)
                    .changeSkin(
                        Constant.SKIN_PATH,
                        null,
                        object : DefaultSkinChangingCallback() {

                            override fun onError(e: Exception) {
                                Toast.makeText(context, "换肤失败:" + e.message, Toast.LENGTH_SHORT)
                                    .show()
                            }

                            override fun onComplete() {
                                Toast.makeText(context, "换肤成功", Toast.LENGTH_SHORT).show()
                            }
                        })
            }
        }
    }


    override fun onDestroy() {
        super.onDestroy()
        SkinManager.getInstance().unregister(this)
    }

    companion object {
        @JvmStatic
        fun newInstance(param1: String = "", param2: String = "") =
            ImageFragment().apply {
                arguments = Bundle().apply {
                    putString(ARG_PARAM1, param1)
                    putString(ARG_PARAM2, param2)
                }
            }
    }
}